﻿package com.calefk.util {
	
	import flash.events.KeyboardEvent;

	public class KeyPress {
		
		public static const KEY_A:int = 65;
		public static const KEY_S:int = 83;
		public static const KEY_D:int = 68;
		public static const KEY_W:int = 87;
		
		public static const KEY_J:int = 74;
		public static const KEY_K:int = 75;
		public static const KEY_L:int = 76;
		public static const KEY_H:int = 72;
		
		public static const KEY_SPACE:int = 32;
		
		private var mOnePressed:Array;
		private var mPress:Array;
		
		public function KeyPress() {
			mOnePressed = new Array();
			mPress = new Array();
		}
		// ------------------------
		// Functions only key press
		// ------------------------
		public function addKeyPress(keyCode:int):void {
			mPress["KEYCODE_" + keyCode] = { code:keyCode , isPress:0 };
		}
		
		public function isKeyPress(keyCode:int):Boolean {
			if (mPress["KEYCODE_" + keyCode]) {
				if (mPress["KEYCODE_" + keyCode].isPress == 1) {
					return true;
				}
			}
			return false;
		}
		
		public function onKeyUp(e:KeyboardEvent):void {
			// check press
			var id:String;
			for (id in mPress) {
				if (e.keyCode == mPress[id].code) {
					mPress[id].isPress  = 0;
					break;
				}
			}
			// check one pressed
			for (id in mOnePressed) {
				if (e.keyCode == mOnePressed[id].code) {
					mOnePressed[id].isPress  = 0;
					mOnePressed[id].isActive = 0;
					break;
				}
			}
		}
		
		public function onKeyDown(e:KeyboardEvent):void {
			var id:String;
			for (id in mPress) {
				if (e.keyCode == mPress[id].code) {
					mPress[id].isPress  = 1;
					break;
				}
			}
			// check one pressed
			for (id in mOnePressed) {
				if (e.keyCode == mOnePressed[id].code) {
					mOnePressed[id].isPress = 1;
					break;
				}
			}
		}
		// -----------------------
		// Function to key Pressed
		// -----------------------
		public function addKeyPressed(keyCode:int , callFunctionExecute:Function):void {
			mOnePressed["KEYCODE_" + keyCode] = {code:keyCode , funct: callFunctionExecute , isPress:0 , isActive:0};
		}
		
		public function updateKeysPressed():void {
			for (var key in mOnePressed) {
				var obj:Object = mOnePressed[key];
				if (obj.isActive == 0 && obj.isPress == 1) {
					// active 
					obj.isActive = 1;
					// call function
					if (obj.funct != null) {
						obj.funct();
					}
				}
			}
			
			/*if (mOnePressed["KEYCODE_" + keyCode]) {
				var obj:Object = mOnePressed["KEYCODE_" + keyCode];
				if (obj.isActive == 0 && obj.isPress == 1) {
					// active 
					obj.isActive = 1;
					// call function
					if (obj.funct != null) {
						obj.funct();
					}
				}
			}*/
		}
		// -----
		// Reset
		// -----
		public function reset():void {
			var id:String;
			for (id in mPress) {
				mPress[id].isPress  = 0;
			}
			
			// check one pressed
			for (id in mOnePressed) {
				mOnePressed[id].isPress  = 0;
				mOnePressed[id].isActive = 0;
			}
		}
		
		public function clean():void {
			var id:String;
			for (id in mPress) {
				mPress[id] = null;
			}
			
			// check one pressed
			for (id in mOnePressed) {
				mOnePressed[id] = null;
			}
		}
	}

}